package com.io.client;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by 41579 on 2017/4/5.
 */
public class TestMain {

    Process process;
    Thread in;
    Thread out;

    public TestMain(Process process) {
        this.process = process;
        final InputStream inputStream
                = process.getInputStream();
        final BufferedReader
                r = new BufferedReader
                (new InputStreamReader(inputStream));
        final byte[] buffer = new byte[1024];
        out = new Thread() {
            String line;
            int lineNumber = 0;

            public void run() {
                try {
                    while (true) {
                        System.out.println
                                ("read.....");
                        int count = inputStream.read(buffer);
                        System.out.println
                                (lineNumber + ":" + new String
                                        (buffer, 0, count - 1));
                        line = r.readLine();
                        System.out.println
                                (lineNumber + ":" + line);
                        lineNumber++;
                    }
                } catch (Exception e) {

                }
            }
        };
        final BufferedReader reader =
                new BufferedReader
                        (new InputStreamReader(System.in));
        final OutputStream outputStream
                = process.getOutputStream();
        in = new Thread() {
            String line;

            public void run() {
                try {
                    while (true) {
                        System.out.println
                                ("write.....");
                        outputStream.write(
                                (reader.readLine() + "/n").getBytes());
                        outputStream.flush();
                    }
                } catch (Exception e) {

                }
            }
        };
    }

    public static void main(String[] args) throws Exception {
//        try {
//            TestMain command =
//                    new TestMain(Runtime.getRuntime().
//                            exec("native2ascii"));
//            command.startIn();
//            command.startOut();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        RandomAccessFile file = new RandomAccessFile("I:\\NIO.txt","rw");
//        FileChannel fileChannel = file.getChannel();
//        ByteBuffer byteBuffer = ByteBuffer.wrap("asdfzxcv".getBytes());
//        fileChannel.write(byteBuffer);
//        ByteBuffer byteBuffer1 = ByteBuffer.allocate(1024);
//        fileChannel.read(byteBuffer1);
//        byteBuffer1.flip();
//        System.out.println(byteBuffer1.position());
//        byte[] bytes = new byte[8];
//        byteBuffer1.get(bytes);
//        System.out.println(new String(bytes));
//        fileChannel.close();
        long size = 0x8ffffff;
        RandomAccessFile file = new RandomAccessFile("I:\\NIO.txt","rw");
        MappedByteBuffer mappedByteBuffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE,0,size);
        for(int i=0;i<size;i++){
            mappedByteBuffer.put((byte)'x');
        }
        System.out.println("finish writting....");
        System.out.println((char)mappedByteBuffer.get(12));
    }

    public void startIn() {
        in.start();
    }

    public void startOut() {
        out.start();
    }

    public void interruptIn() {
        in.interrupt();
    }

    public void interruptOut() {
        out.interrupt();
    }

}
